//
//  ZJChangeProjectVC.m
//  英伦府邸
//
//  Created by IOS_DEVELOPER on 2017/8/28.
//  Copyright © 2017年 海成集团. All rights reserved.
//

#import "ZJChangeProjectVC.h"
#import "ZJChangeProjectView.h"
@interface ZJChangeProjectVC ()
@property (nonatomic,strong) ZJChangeProjectView * ChangeProjectView;
@property (strong, nonatomic) UIButton *btn;
@property (strong, nonatomic)UIAlertController *alertController;
@end

@implementation ZJChangeProjectVC
-(ZJChangeProjectView *)ChangeProjectView{
    if (!_ChangeProjectView) {
        _ChangeProjectView = [[[NSBundle mainBundle] loadNibNamed:@"ZJChangeProjectView" owner:self options:nil] lastObject];
        
    }
    return _ChangeProjectView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ChangeProjectView.frame = self.view.frame;
    [self.ChangeProjectView.btnWith1000 addTarget:self action:@selector(changePro_id:) forControlEvents:UIControlEventTouchUpInside];
    [self.ChangeProjectView.btnWith1001 addTarget:self action:@selector(changePro_id:) forControlEvents:UIControlEventTouchUpInside];
    self.view.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.9];
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 40, 30, 40, 40)];
    [btn setImage:[UIImage imageNamed:@"nav_icon_gb"] forState:0];
    [btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    self.btn = btn;
    [self.ChangeProjectView addSubview:btn];
    [self.view addSubview:self.ChangeProjectView];
}
#warning 项目改动
-(void)changePro_id:(UIButton *)sender{
    NSString *Projectid = allConstProjectid;
    NSString *NavImgName = allConstNavImgName;
    NSString *HexColor = allConstHexColor;
    switch (sender.tag) {
        case 1000:
        {
            NavImgName = @"nav_bg";
            HexColor = @"e85855";
            Projectid = @"7";
            allConstDocPath = @"nbsyBundle.bundle";
        }
            break;
            
        case 1001:
        {
            HexColor = @"5750e7";
            NavImgName = @"nav_bgg";
            Projectid = @"6";
            allConstDocPath = @"xgsyBundle.bundle";
        }
            break;
        default:
            break;
    }
    if ([Projectid isEqualToString:allConstProjectid]) {

        
         [self dismissViewControllerAnimated:YES completion:nil];
        
        return;
    }
    allConstProjectid = Projectid;
    allConstHexColor = HexColor;
    allConstNavImgName = NavImgName;
    
    NSString *verify = [USER_DEFAULTS objectForKey:@"verify1"];
    allConstMD5id = [Utils md5:[NSString stringWithFormat:@"%@_%@|%@|%@",allConstApp_id,allConstProjectid,allConstMemberid,verify]];
    [USER_DEFAULTS setObject:allConstProjectid forKey:@"allConstProjectid"];
    [USER_DEFAULTS setObject:allConstMD5id forKey:@"verify"];
    [USER_DEFAULTS setObject:allConstHexColor forKey:@"allConstHexColor"];
    [USER_DEFAULTS setObject:allConstNavImgName forKey:@"allConstNavImgName"];
    [USER_DEFAULTS setObject:allConstDocPath forKey:@"allConstDocPath"];
    [USER_DEFAULTS synchronize];

    [self toMainVC];

}
-(void)close{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)toMainVC
{
    NSMutableArray *mutableCopyArr = [[USER_DEFAULTS mutableArrayValueForKey:@"projArr"] mutableCopy];
    
    if (![mutableCopyArr containsObject:allConstProjectid]) {
        
        [mutableCopyArr addObject:allConstProjectid];
        [USER_DEFAULTS setObject:mutableCopyArr forKey:@"projArr"];
        [USER_DEFAULTS synchronize];
        
        NSDictionary *parameter = [LRNetwork requestParameter:@{}];
        NSString *url = [LRNetwork requestURL:@"cgi/reg_project.php"];
        
        [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
            allOpen_promoter = returnValue[@"open_promoter"];
            if (_delegate != nil) {
                [_delegate changeProject];
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
            [HUD showErrorMsg:errorMessage];
        } withFailureBlock:^{
            
        }];
        
    }else{
        NSLog(@"该项目已绑定过了");
        if (_delegate != nil) {
            [_delegate changeProject];
        }

        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}
-(void)hide{
    
    [self.alertController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
